<template>
    <!-- 修改资料 -->
    <div>
                <!-- 内容主题区域 -->
                <el-form label-width="100px" :model="form" label-position="right" ref="formRef" :rules="formRules">
                    <el-form-item label="数据权限名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="数据范围类型" prop="dataScope">
                        <el-select v-model="form.dataScope">
                            <el-option v-for="item in configData.dataScopeList" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="部门选择" prop="deptIds" v-if="form.dataScope!='5'">
                        <el-tree-select v-model="form.deptIdArray" :data="deptData" multiple show-checkbox
                            :render-after-expand="false" :check-strictly=true />
                    </el-form-item>

                    <tsCard header="人员详情" v-if="form.dataScope!='4'">
                        <el-form-item label="人员选择" prop="employeeRows" >
                        <sc-table-select v-model="employeeRows" :apiObj="apiObj" :fileName="fileName" :table-width="700"
                            multiple clearable collapse-tags collapse-tags-tooltip :props="tableProps" @change="change">
                        </sc-table-select>
                    </el-form-item>
                        <el-table :data="configData.employeeList">
                            <template v-for="(item, index) in configData.empolyeeTableConfig" :key="index">
                                <el-table-column :prop="item.prop" :label="item.label" />
                            </template>
                        </el-table>
                    </tsCard>
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check"
                            :inactive-icon="Close" active-value="normal" inactive-value="disable" />
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>

                </el-form>
                <div class="button-position">
                    <el-button @click="updateVisible">取 消</el-button>
                    <el-button type="primary" @click="updateData(formRef)">确 定</el-button>
                </div>
    </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { Check, Close } from '@element-plus/icons-vue';
import { ElNotification} from 'element-plus';
import tsCard from '@/components/tsCard/index.vue'
import Api from '@/api/model/system'
import userApi from '@/api/model/user'
const emit = defineEmits(['updatetable'])
// 添加用户数据的对象
const form = ref({});
const tableProps = {
    label: 'name',
    value: 'id',
}
const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {}
    }
})
//tableselect相关属性
const apiObj = ref(Api.select.query)
const fileName = ref('ChooseEmployeeReport')
const employeeRows = ref([])

const deptData = ref({})
const configData = ref({})
const formRef = ref()
const formRules = reactive({
})

onMounted(() => {
    getInit()
})

const getInit = async () => {
    let res = await Api.datapermission.detail.post(props.paramsData);
    if (res.code == 200) {
        configData.value = res.configData
        employeeRows.value = res.configData.employeeList
        form.value = res.bean
    }
    let params = {
        fileName: 'DeptTreeReport'
    }
    let userRes = await userApi.dept.DeptTree.post(params);
    deptData.value = userRes.reportData
}

const restAddFormData = () => {
    formRef.value.resetFields();
}

const updateVisible = (val) => {
    emit('updatetable')
}

const change = (val) => {
    configData.value.employeeList = val
    val.forEach((item) => {
        if(form.value.employeeIdArray.indexOf(item) == -1){
            form.value.employeeIdArray.push(item.id)
        }
    });
}

// 上传
const updateData = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            update();
        }
    })
}

const update = async () => {
    form.value.employeeIdArray=[...new Set(form.value.employeeIdArray)]
    var params = {
        bean: form.value,
    }
    var res = await Api.datapermission.update.post(params);
    if (res.code == 200) {
        restAddFormData()
        ElNotification.success({ title: '', message: '添加成功！', duration: 2000 });
        emit('updatetable',true)
    }
    else {
        ElNotification.error({ title: '添加失败', message: res.msg });
    }
}



</script>

<style lang="less" scoped>
:deep(.el-select){
    width: 100%;
}
</style>

